package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    /**
     * 请求 http://localhost:18081/user
     * 提交方式：POST
     */
    @PostMapping
    public int insert(@RequestBody User user) {
        userService.insert(user);
        return user.getId();
    }

    /**
     * 请求 http://localhost:18081/user
     * 提交的方式：PUT
     */
    @PutMapping
    public int update(@RequestBody User user) {
        return userService.update(user);
    }

    /**
     * 请求 http://localhost:18081/user/1
     * 提交的方式：DELETE
     */
    @DeleteMapping("/{id}")
    public int delete(@PathVariable("id") Integer id) {
        return userService.delete(id);
    }

    /**
     * 请求 http://localhost:18081/user/1
     */
    @GetMapping("/{id}")
    public User info(@PathVariable("id") Integer id) {
//        return new User(1, "张三", "深圳");
        return userService.info(id);
    }

}
